// 1, 获取原来数组的方法
let oldArrayProtoMethods = Array.prototype;

// 2, 继承
let ArrayMethods = Object.create(oldArrayProtoMethods);

// 劫持对原数组造成影响的函数
let methods = [
  'push',
  'pop',
  'unshift',
  'shift',
  'splice',
  'reverse',
  'sort'
]

methods.forEach(item => {
  ArrayMethods[item] = function(...args){
    let result = oldArrayProtoMethods[item].apply(this, args);
    let inserted;
    // 数组追加对象的情况
    switch(item) {
      case 'push':
      case 'unshift':
        inserted = args;
        break;
      case 'splice':
        inserted = args.splice(2);
        break;
    }
    // ob为Observer实例
    let ob = this.__ob__;
    if (inserted) {
      ob.observeArray(inserted);
    }
    ob.dep.notify();
    return result;
  }
})

export default ArrayMethods;